﻿using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;

namespace EM.OA.WebCommon.Base
{
    /// <summary>
    /// ViewModel基类
    /// </summary>
    public class BaseViewModel
    {
        /// <summary>
        /// 创建人
        /// </summary>
        public Guid? CreateBy { get; set; }

        /// <summary>
        /// 创建人姓名
        /// </summary>
        public string CreateByName { get; private set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public Guid? UpdateBy { get; set; }

        /// <summary>
        /// 修改人姓名
        /// </summary>
        public string UpdateByName { get; private set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }

        /// <summary>
        /// ViewModel转换为DBEntity,用于增加实体
        /// </summary>
        /// <typeparam name="TEntity">DBEntity</typeparam>
        /// <returns></returns>
        public TEntity ToEntity<TEntity>() where TEntity : class,new()
        {
            TEntity entity = new TEntity();
            entity.GetType().GetProperties().ToList().ForEach(p =>
            {
                PropertyInfo property = this.GetType().GetProperty(p.Name);
                if (property != null)
                {
                    object value = property.GetValue(this);
                    p.SetValue(entity, value);
                }
            });
            return entity;
        }

        /// <summary>
        /// ViewModel转换为DBEntity,用于修改实体
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="entity">实体信息</param>
        /// <returns></returns>
        public TEntity ToEntity<TEntity>(TEntity entity) where TEntity : class,new()
        {
            entity.GetType().GetProperties().ToList().ForEach(p =>
            {
                if (p.Name != "CreateBy" && p.Name != "CreateTime")
                {
                    PropertyInfo property = this.GetType().GetProperty(p.Name);
                    if (property != null)
                    {
                        object value = property.GetValue(this);
                        p.SetValue(entity, value);
                    }
                }
            });
            return entity;
        }

        /// <summary>
        /// DBEntity转换为ViewModel
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public BaseViewModel ToModel<TEntity>(TEntity entity) where TEntity : class,new()
        {
            this.GetType().GetProperties().ToList().ForEach(p =>
            {
                PropertyInfo property = entity.GetType().GetProperty(p.Name);
                if (property != null)
                {
                    object value = property.GetValue(entity);
                    p.SetValue(this, value);
                }
            });
            UserBLL userBll = new UserBLL();
            if (CreateBy.HasValue)
            {
                EMOA_User user = userBll.GetEntity(CreateBy.Value);
                if (user != null)
                    CreateByName = user.EmployeeName;
                else
                    CreateByName = "Admin";
            }
            if (UpdateBy.HasValue)
            {
                EMOA_User user = userBll.GetEntity(UpdateBy.Value);
                if (user != null)
                    UpdateByName = user.EmployeeName;
                else
                    UpdateByName = "Admin";
            }

            return this;
        }


        /// <summary>
        /// 查询下拉列表的键值
        /// </summary>
        /// <param name="dropdownListType"></param>
        /// <returns></returns>
        public virtual IEnumerable<SelectListItem> GetListItems(string code, string defaultValue = null)
        {
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Text = "===请选择===", Value = "", Selected = true } };
            DictionaryBLL bll = new DictionaryBLL();
            List<EMOA_Dictionary> list = bll.GetDicList(code);
            list.ForEach(p =>
            {
                items.Add(new SelectListItem
                {
                    Text = p.DictionaryName,
                    Value = p.DictionaryCode
                });
            });
            if (!string.IsNullOrEmpty(defaultValue))
            {
                SelectListItem item = items.FirstOrDefault(p => p.Value == defaultValue);
                if (item != null)
                {
                    item.Selected = true;
                }
            }
            return items;
        }

        /// <summary>
        /// 查询下拉列表的键值（Value值是不是该是GUID）
        /// </summary>
        /// <param name="dropdownListType"></param>
        /// <returns></returns>
        public virtual IEnumerable<SelectListItem> GetListItems2(string code, string defaultValue = null)
        {
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Text = "===请选择===", Value = "", Selected = true} };
            DictionaryBLL bll = new DictionaryBLL();
            List<EMOA_Dictionary> list = bll.GetDicList(code);
            list.ForEach(p =>
            {
                items.Add(new SelectListItem
                {
                    Text = p.DictionaryName,
                    Value = p.DictionaryID.ToString()
                });
            });
            if (!string.IsNullOrEmpty(defaultValue))
            {
                SelectListItem item = items.FirstOrDefault(p => p.Value == defaultValue);
                if (item != null)
                {
                    item.Selected = true;
                }
            }
            return items;
        }
    }
}
